| Total Complexity | 3 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { Inject } from '@nestjs/common'; |
||
| 8 | |||
| 9 | @CommandHandler(UpdateLeaveRequestCommand) |
||
| 10 | export class UpdateLeaveRequestCommandHandler { |
||
| 11 | constructor( |
||
| 12 | @Inject('ILeaveRequestRepository') |
||
| 13 | private readonly leaveRequestRepository: ILeaveRequestRepository, |
||
| 14 | private readonly doesLeaveRequestBelongToUser: DoesLeaveRequestBelongToUser |
||
| 15 | ) {} |
||
| 16 | |||
| 17 | public async execute(command: UpdateLeaveRequestCommand): Promise<string> { |
||
| 18 | const { |
||
| 19 | id, |
||
| 20 | type, |
||
| 21 | startDate, |
||
| 22 | startsAllDay, |
||
| 23 | endDate, |
||
| 24 | endsAllDay, |
||
| 25 | comment, |
||
| 26 | user |
||
| 27 | } = command; |
||
| 28 | |||
| 29 | const leaveRequest = await this.leaveRequestRepository.findOneById(id); |
||
| 30 | |||
| 31 | if (!leaveRequest) { |
||
| 32 | throw new LeaveRequestNotFoundException(); |
||
| 33 | } |
||
| 34 | |||
| 35 | if ( |
||
| 36 | false === |
||
| 37 | this.doesLeaveRequestBelongToUser.isSatisfiedBy(leaveRequest, user) |
||
| 38 | ) { |
||
| 39 | throw new LeaveRequestCantBeUpdatedException(); |
||
| 40 | } |
||
| 41 | |||
| 42 | leaveRequest.update( |
||
| 43 | type, |
||
| 44 | startDate, |
||
| 45 | startsAllDay, |
||
| 46 | endDate, |
||
| 47 | endsAllDay, |
||
| 48 | comment |
||
| 49 | ); |
||
| 50 | await this.leaveRequestRepository.save(leaveRequest); |
||
| 51 | |||
| 52 | return leaveRequest.getId(); |
||
| 53 | } |
||
| 55 |